// ==UserScript==
// @name         Duowan Controller
// @namespace    https://gitee.com/Sisbian/DuowanController
// @version      0.3
// @description  try to take over the world!
// @author       You
// @match        http://tu.duowan.com/gallery/*
// @grant        none
// @homepageURL  https://gitee.com/Sisbian/DuowanController
// @supportURL   https://gitee.com/Sisbian/DuowanController/issues
// @updateURL    https://gitee.com/Sisbian/DuowanController/raw/master/src/core.user.js
// @@downloadURL https://gitee.com/Sisbian/DuowanController/raw/master/src/core.user.js
// ==/UserScript==

(function () {
    'use strict';
    //视频最大高度,无需更改请写0
    const MAX_VIDEO_HEIGHT = 800;
    if (MAX_VIDEO_HEIGHT) { //设置视频最大高度
        let innerStyleText = `\n\t#show-img > video {\n\t\tmax-height: ${MAX_VIDEO_HEIGHT}px;\n\t}\n`;
        let headElement = document.getElementsByTagName("head").item(0);
        let newStyleElement = document.createElement("style");
        newStyleElement.setAttribute("type", "text/css");
        let styleTextNode = document.createTextNode(innerStyleText);
        newStyleElement.appendChild(styleTextNode);
        headElement.appendChild(newStyleElement);
    }
    let btnPrev = document.querySelector('#show-img > a.prev');
    let btnNext = document.querySelector('#show-img > a.next');
    //监听按键结束事件
    document.onkeyup = function () {
        if (document.play_video_lock) {
            delete document.play_video_lock;
        }
    };
    //监听按键触发事件
    document.onkeydown = function (keyEvent) {
        let videoNode = document.querySelector('#show-img > video');
        let btnPlay = document.querySelector('#show-img > i.btn-play');
        if (document.imitate_clock) {
            return;
        }
        if ("ArrowRight" === keyEvent.code) {
            //上锁
            document.imitate_clock = true;
            //模拟点击下一个
            btnNext.click();
        } else if ("ArrowLeft" === keyEvent.code) {
            //上锁
            document.imitate_clock = true;
            //模拟点击下一个
            btnPrev.click();
        } else if (keyEvent.ctrlKey && btnPlay && !document.play_video_lock) {
            document.play_video_lock = true;
            playSwitch(videoNode, btnPlay);
        }
        if (document.imitate_clock) {
            //如果上锁则设置解锁时间
            setTimeout(function () {
                document.imitate_clock = false;
            }, 500);
        }
    };
    //视频播放开关
    function playSwitch(videoNode, btnPlay) {
        if (videoNode) {
            if (videoNode.paused) {
                btnPlay.hidden = true;
                videoNode.play();
            } else {
                btnPlay.hidden = false;
                videoNode.pause();
            }
        }
    }
})();